package cn.vcjf.washcar.api.user.controller;


import cn.vcjf.washcar.api.user.entity.UserCar;
import cn.vcjf.washcar.api.user.service.UserCarService;
import cn.vcjf.washcar.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 用户汽车表 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-23
 */
@Controller
@RequestMapping("/user/userCar")
public class UserCarController {
	@Resource
	private UserCarService userCarService;

	@RequestMapping("/addUserCar")
	@ResponseBody
	public TableResult addUserCar(UserCar userCar) {
		userCar.preInsert();
		userCarService.saveUserCar(userCar);
		return TableResult.Result();
	}

	@RequestMapping("/updateUserCar")
	@ResponseBody
	public TableResult updateUserCar(UserCar userCar) {
		userCar.preUpdate();
		boolean b = userCarService.updateUserCarById(userCar);
		return TableResult.Result(b);
	}

	@RequestMapping("/findUserCarOne")
	@ResponseBody
	public TableResult findUserCarOne(UserCar userCar) {

		EntityWrapper<UserCar> wrapper = new EntityWrapper<UserCar>();

		if (userCar.getId() != null) {
			wrapper.eq("id", userCar.getId());
		}
		//Todo

		UserCar result = userCarService.selectOne(wrapper);

		return TableResult.Result(result);
	}

	@RequestMapping("/findUserCarList")
	@ResponseBody
	public TableResult findUserCarList(UserCar userCar) {

		EntityWrapper<UserCar> wrapper = new EntityWrapper<UserCar>();

		if (userCar.getId() != null) {
			wrapper.eq("id", userCar.getId());
		}
		if (userCar.getUserId() != null) {
			wrapper.eq("userId", userCar.getUserId());
		}
		wrapper.orderBy("sort");
		wrapper.orderBy("defaultCar", false);
		Page<UserCar> page = new Page<UserCar>(userCar.getPage(), userCar.getLimit());

		Page<UserCar> result = userCarService.selectPage(page, wrapper);

		return TableResult.Result(result);

	}

	@RequestMapping("/deleteUserCar")
	@ResponseBody
	public TableResult deleteUserCar(UserCar userCar) {
		userCar.preUpdate();
		boolean b = userCarService.deleteById(userCar.getId());
		return TableResult.Result(b);
	}

}
